<?php
/**
 * @filesource:					PlaceDistrictModel.class.php
 * @desc:						墓地模型类
 *
 * @author:						zhangzhao
 * @copyright:					zhangzhao@huigeyuan.com
 * @date:						2011-05-05
 */

class PlaceDistrictModel extends Model{

	protected $_validate = array(
		array('name','require', '名不能为空！')

		);

	protected $_auto = array(
		array('status', 1)//自动填充墓地状态为未验收

	);
     public function checkPlaceDistrict() {
		$map['name']	 =	 $_POST['name'];
		$map['pid']	=	isset($_POST['pid'])?$_POST['pid']:0;
        $map['status'] = 1;
        if(!empty($_POST['id'])) {
			$map['id']	=	array('neq',$_POST['id']);
        }
		$result	=	$this->where($map)->field('id')->find();
        if($result) {
        	return false;
        }else{
			return true;
		}
	}

	public function getRootPlaceDistrictId(){
		$list = $this->where(array('level'=>1))->field('id')->find();
		return $list['id'];
	}

	public function getRootPlaceDistrictLevel(){
		return 1;
	}

	public function getLevel($parent_id){
		$list = $this->where(array('id'=>$parent_id))->field('level')->find();
		return $list['level'] + 1;
	}

	//获取权限树
	//暂时获取二级节点树
	public function getPlaceDistrictTree($PlaceDistrict_id = ''){
		if($PlaceDistrict_id == ''){
			$PlaceDistrict_id = $this->getRootPlaceDistrictId();
		}
		$root_level = $this->getRootPlaceDistrictLevel();

		$level_1_PlaceDistricts = $this->where('level='.($root_level+1).' and pid='.$PlaceDistrict_id)->order('sort')->select();

		$PlaceDistrict_tree = array();
		foreach($level_1_PlaceDistricts as  $PlaceDistrict){
			$PlaceDistrict_tree[$PlaceDistrict['id']] = $PlaceDistrict;
			$PlaceDistrict_tree[$PlaceDistrict['id']]['child'] = array();
			$level_2_PlaceDistricts = $this->where('level='.($root_level+2).' and pid='.$PlaceDistrict['id'])->order('sort')->select();
			if($level_2_PlaceDistricts != NULL){
				$PlaceDistrict_tree[$PlaceDistrict['id']]['child'] = $level_2_PlaceDistricts;
			}
		}

		return $PlaceDistrict_tree;
	}


}

?>